{extend name="common/common" /}

{block name="style"}
<style type="text/css">
.container-fluid .btn{
    float: right;
    margin-left: 10px;
}
#cusTable tr{
	cursor: pointer;
}
</style>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>产品列表</h5>
        </div>
        <div class="ibox-content">
            <form class="layui-form" action="" id="seaForm">
                <div class="layui-input-inline" style="width: 200px;">
                    <input type="text" name="searchText" id="searchText" placeholder="产品名/学校名/专业名/分类名" class="layui-input">
                </div>

                <div class="layui-inline">
                    <button type="button" id="search" class="btn btn-info" style="margin-top: 2px;">查询</button>
                </div>
            </form>
            <div class="container-fluid">
                <div class="container-fluid">
                    {if authCheck('product/productdel')}
                    <a class="btn btn-danger addproconfig getIDs" href="javascript:delPro();" data-param=''>删除产品</a>
                    {/if}
                    {if authCheck('product/productadd')}
                    <a class="btn btn-primary" href="productadd">添加产品</a>
                    {/if}
                </div>
            </div>
            <table id="cusTable">
                <thead>
                    <th>checkbox</th>
                    <th class="text-center" data-field="id">ID</th>
                    <th class="text-center" data-field="name">产品名称</th>
                    <th class="text-center" data-field="c_name">所属分类</th>
                    <th class="text-center" data-field="school">学校名称</th>
                    <th class="text-center" data-field="major">专业名称</th>
                    <th class="text-center" data-field="age_limit">年限</th>
                    <th class="text-center" data-field="tuition">学费</th>
                    {if authCheck('product/productdel')}
                    <th class="text-center" data-field="operate">操作</th>
                    {/if}
                </thead>
            </table>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script type="text/javascript">
var IDs = '';
function initTable() {
    //先销毁表格
    $('#cusTable').bootstrapTable('destroy');
    //初始化表格,动态从服务器加载数据
    $("#cusTable").bootstrapTable({
        method: "get",  //使用get请求到服务器获取数据
        url: "./index", //获取数据的地址
        striped: true,  //表格显示条纹
        pagination: true, //启动分页
        pageSize: 10,  //每页显示的记录数
        pageNumber:1, //当前第几页
        pageList: [5, 10, 15, 20, 25],  //记录数可选列表
        sidePagination: "server", //表示服务端请求
        paginationFirstText: "首页",
        paginationPreText: "上一页",
        paginationNextText: "下一页",
        paginationLastText: "尾页",
        queryParamsType : "undefined",
        columns : [{
            checkbox: true,
            formatter:function(value,row,index){
                if (row.state == true){
                    return {
                        checked : value,//设置选中
                    };
                }
                return value;
            },
        }],
        queryParams: function queryParams(params) {   //设置查询参数
            var param = {
                pageNumber: params.pageNumber,
                pageSize: params.pageSize,
                seaGrade:$("#seaGrade").val(),
                seaSystem:$("#seaSystem").val(),
                seaPay:$("#seaPay").val(),
                seaEdu:$("#seaEdu").val(),
                searchText:$("#searchText").val()
            };
            return param;
        },
        onLoadSuccess: function(res){  //加载成功时执行
            if(111 == res.code){
                window.location.reload();
            }
            layer.msg("加载成功", {time : 1000});
        },
        onLoadError: function(){  //加载失败时执行
            layer.msg("加载数据失败");
        },
        onDblClickRow: function (row) {
            editbox = layer.open({
                type:2,
                title:'产品详情',
                area:['80%','80%'],
                content:"{:url('showPro')}?pro_id="+row.id,
                btn:["确定"],
                yes:function(){
                    layer.closeAll();
                    initTable();
                }
            });
        }
    });
}

$(function(){
    //调用函数，初始化表格
    initTable();

    layui.use(['form'], function(){
        var form = layui.form;
    });

    //当点击查询按钮的时候执行
    $("#search").bind("click", initTable);

    $('.getIDs').on('click',function(){
        var checkedOn= $('#cusTable').bootstrapTable('getSelections');
        var IDs = '';
        for(var i=0;i<checkedOn.length;i++){
            if(i == 0){
                IDs = checkedOn[0].id;
            }else{
                IDs += ',' + checkedOn[i].id;
            }
        }
        $('.getIDs').attr('data-param',IDs);
    })
});

function delPro(ids=null){
    if(ids == null){
        ids = $('.getIDs').attr('data-param');
    }
    if(ids != ''){
        layer.confirm('确认删除这些数据?', {icon: 3, title:'提示'}, function(index){
            $.post('productDel',{ids:ids},function(res){
                if(res.code == 1){
                    layer.alert(res.msg, {title: '友情提示', icon: 1, closeBtn: 0}, initTable);
                }else{
                    layer.msg(res.msg, {anim: 6});
                }
            });
        });
    }else{
        layer.msg("请选择数据", {anim: 6});
    }
}


</script>
{/block}
